# /**
# * Copyright (c) 2022 Beijing Jiaotong University
# * PhotLab is licensed under [Open Source License].
# * You can use this software according to the terms and conditions of the [Open Source License].
# *
# * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
# *
# * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# *
# * See the [Open Source License] for more details.
# */
# /**
# * Author: Shihao Xie
# * Created: Jul. 1, 2022
# * Supported by: National Key Research and Development Program of China
# */
import logging
import os
import sys
__dir__ = os.path.dirname(os.path.abspath(__file__))
# print(f'{__dir__= }')
# sys.path.append(__dir__)
# sys.path.append(os.path.abspath(os.path.join(__dir__, '..../')))
# sys.path.append(os.path.abspath(os.path.join(__dir__, '../../pyphot-engine/')))
# sys.path.append(os.path.abspath(os.path.join(__dir__, '../../pyphot/')))
sys.path.append(os.path.abspath(os.path.join(__dir__, '../../photfdtd/')))
sys.path.append(os.getcwd())
# print(f'{sys.path= }')
from phot_server.server_config.global_config import IP, PORT, PROTOCOL, roots_rel_to_abs
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TJSONProtocol
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TCompactProtocol
from thrift.server import TServer
from phot_server.core.main_handler import MainHandler
from phot_server.core.main_processor import MainProcessor
from phot_server.utils.phot_logger import logger
from phot_server.service import *
# from phot_server.database.minio_server import start_minio_server

__server = None
def server(ip, port, protocol):
    # Set service handler
    handler = MainHandler()
    processor = MainProcessor(handler)

    # Set global root config
    roots_rel_to_abs()

    # Set ip and port
    transport = TSocket.TServerSocket(ip, port)

    # Set thrift transport protocol
    transport_factory = TTransport.TBufferedTransportFactory()
    if "TBinaryProtocol" == protocol:
        protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
    elif "TJSONProtocol" == protocol:
        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
    elif "TCompactProtocol" == protocol:
        protocol_factory = TCompactProtocol.TCompactProtocolFactory()
    else:
        protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
    phot_server = TServer.TThreadPoolServer(processor, transport, transport_factory, protocol_factory)
    __server = phot_server

    # Set thread number
    phot_server.setNumThreads(16)

    logging.info("PyPhotServer is Running!")
    logger.info("PyPhotServer is Running!")
    # start_minio_server()
    __server.daemon = True
    phot_server.serve()
    logger.info("PyPhotServer is Ended!")

if __name__ == "__main__":
    # argv = [ip, port, protocol]
    if len(sys.argv) == 4:
        server(sys.argv[1], sys.argv[2], sys.argv[3])
    else:
        server(IP, PORT, PROTOCOL)

